package com.practice.springboot_01_zwj.controller;

import com.github.pagehelper.PageInfo;
import com.practice.springboot_01_zwj.entity.AccountInfo;
import com.practice.springboot_01_zwj.entity.CloudNetwork;
import com.practice.springboot_01_zwj.service.AccountInfoService;
import com.practice.springboot_01_zwj.entity.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * ClassName: AccountInfoController <br/>
 * Description: 针对AccountInfo表操作的控制器类 <br/>
 * date: 2024/1/22 20:45 <br/>
 *
 * @author mky <br/>
 * @version v1.0 <br/>
 * @since JDK 17 <br/>
 */
@RestController
@CrossOrigin(value = "http://localhost:8083", allowCredentials = "true")
//测试一下
public class AccountInfoController {
    @Autowired
    private AccountInfoService accountInfoService;
    @PostMapping("accountInfo/selectAll")
    public List<AccountInfo> selectAll(@RequestBody AccountQueryRequest request) {
        // 处理空参数情况
        if (request.getAcName() == null || request.getAcName().trim().isEmpty()) {
            return Collections.emptyList();
        }
        return accountInfoService.selectAll(request.getAcName());
    }
    @PostMapping("accountInfo/selectCityName")
    public List<AccountInfo> selectCityName(@RequestBody AccountQueryRequest request) {
        // 处理空参数情况
        if (request.getCityName() == null || request.getCityName().trim().isEmpty()) {
            return Collections.emptyList();
        }
        return accountInfoService.selectCityName(request.getCityName());
    }

    // 创建请求体DTO
    public static class AccountQueryRequest {
        private String acName;
        private String cityName;
        // 必须有无参构造函数
        public AccountQueryRequest() {
        }

        public String getCityName() {
            return cityName;
        }

        public void setCityName(String cityName) {
            this.cityName = cityName;
        }

        // Getter 和 Setter
        public String getAcName() {
            return acName;
        }
        public void setAcName(String acName) {
            this.acName = acName;
        }
    }

//    /**
//     * 账号列表的分页查询
//     * @param aname
//     *   查询条件账号名称
//     * @param currentPage
//     *   当前页
//     * @param pageSize
//     *   每页条数
//     * @return
//     *   返回统一格式
//     */
//    @GetMapping("/selectAllAccountInfo")
//    public R<PageInfo<AccountInfo>> selectAll(@RequestParam(defaultValue = "")String aname,
//                                              @RequestParam(defaultValue = "1") int currentPage,
//                                              @RequestParam(defaultValue = "3")int pageSize) {
//        return new R<>(Result.SUCCESS.getCode(),
//                Result.SUCCESS.getMsg(),
//                accountInfoService.selectAll(aname,currentPage,pageSize));
//    }

    /**
     * 添加账号
     * @param accountInfo
     *   添加的账号对象
     * @return
     *   统一返回格式
     */
    @PostMapping("accountInfo/addAccountInfo")
    public R<Integer> addAccountInfo(@RequestBody AccountInfo accountInfo) {
        try {
            accountInfo.setDelMark(1);

            int result = accountInfoService.addAccountInfo(accountInfo);
            if(result > 0){
                return R.success("添加成功",accountInfo.getUserId());
            }else {
                return R.error("添加失败");
            }
        }catch (Exception e){
            return R.error("系统错误" + e.getMessage());
        }
    }

    /**
     * 更新账号
     * @param accountInfo
     *   更新的账号对象
     * @return
     *   统一返回格式
     */
    @PutMapping("accountInfo/updateAccountInfo")
    public R<String> updateAccountInfo(@RequestBody AccountInfo accountInfo) {
        try {
            // 添加参数验证
            if (accountInfo.getUserId() == null) {
                return R.error(400, "更新失败：任务ID不能为空");
            }

            int result = accountInfoService.updateAccountInfo(accountInfo);
            if (result > 0) {
                return R.success("更新成功", null);
            } else {
                return R.error("更新失败，可能原因：任务不存在或数据未变化");
            }
        } catch (Exception e) {
            return R.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 删除账号（逻辑删除）
     * @param userId
     *    删除的账号id
     * @return
     *    统一返回格式
     */
    @DeleteMapping("accountInfo/deleteAccountInfo/{userId}")
    public R<Integer> deleteAccountInfo(@PathVariable Integer userId) {
        try {
            int result = accountInfoService.deleteAccountInfo(userId);
            if (result > 0){
                return R.success("删除成功",null);
            } else {
                return R.error("删除失败");
            }
        }catch (Exception e){
            return R.error("系统错误: " + e.getMessage());
        }
//        return accountInfoService.deleteAccountInfo(userId);
//        return new R<>(Result.SUCCESS.getCode(),
//                Result.SUCCESS.getMsg()
//                , accountInfoService.deleteAccountInfo(id));
    }

//    @GetMapping("/updateApass")
//    public R<Integer> updateApass(@RequestParam("apass") String apass,
//                                  @RequestParam("empId") Integer empId) {
//        return new R<>(Result.SUCCESS.getCode(), Result.SUCCESS.getMsg(), accountInfoService.updateApass(apass, empId));
//    }
    @GetMapping("accountInfo/selectAcNameAndUserName")
    public PageInfo<AccountInfo> selectAcNameAndUserName(
            @RequestParam(required = false, defaultValue = "") String acName, // 添加默认值
            @RequestParam(required = false, defaultValue = "") String userName, // 添加默认值
            @RequestParam(required = false, defaultValue = "") String cityName, // 添加默认值
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size
    ) {
        return accountInfoService.selectAcNameAndUserName(acName, userName,cityName,page, size);
    }
}
